package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.im.MybatisPlusConfig;
import com.liveinstars.im.domain.dao.mapper.MessageGroupMapper;
import com.liveinstars.im.domain.dao.model.MessageGroup;
import com.liveinstars.im.domain.manager.MessageGroupManager;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-09
 */
@Service("messageGroupManager")
public class MessageGroupManagerImpl extends ServiceImpl<MessageGroupMapper, MessageGroup> implements MessageGroupManager {

    private final static Logger logger = LoggerFactory.getLogger(MessageGroupManagerImpl.class);

    @Resource
    private MessageGroupMapper messageGroupMapper;

    public static final String MESSAGE_GROUP = "message_group";

    private String getUCGTableName(Long index) {
        return MESSAGE_GROUP + index % 10;
    }

    @Override
    public MessageGroup getById(Long chatGroupId, Long msgId) {
        MybatisPlusConfig.myTableName.set(getUCGTableName(chatGroupId));
        MessageGroup messageGroup = this.messageGroupMapper.selectById(msgId);
        return messageGroup;
    }

    @Override
    public IPage<MessageGroup> pageByChatGroupId(Long chatGroupId, Integer pageNum, Integer pageSize) {
        MybatisPlusConfig.myTableName.set(getUCGTableName(chatGroupId));
        IPage<MessageGroup> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MessageGroup> queryWrapper = new QueryWrapper<>();
        if (chatGroupId != null) {
            queryWrapper.eq("chat_group_id", chatGroupId);
        }
        queryWrapper.in("content_type", 0,1).orderByDesc("date");
        this.messageGroupMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public void deleteByChatGroupIdAndMsgId(Long chatGroupId, Long msgId) {
        try {
            MybatisPlusConfig.myTableName.set(getUCGTableName(chatGroupId));
            this.messageGroupMapper.deleteById(msgId);
        } catch (Exception e) {
            logger.error("删除圈子消息异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void saveByGhatGroupId(MessageGroup messageGroup) {
        try {
            MybatisPlusConfig.myTableName.set(getUCGTableName(messageGroup.getChatGroupId()));
            logger.info("saveByGhatGroupId {}", messageGroup.toString());
            this.messageGroupMapper.insertUtf8mb4(messageGroup);
        } catch (Exception e) {
            logger.error("保存圈子消息异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
